<?php
declare(strict_types=1);
use JetBrains\PhpStorm\NoReturn;
use Yaf\Plugin_Abstract;
use Yaf\Request_Abstract;
use Yaf\Response_Abstract;

class ExceptionHandlerPlugin extends Plugin_Abstract
{
    /**
     * 异常处理
     * @throws Exception
     */
    public function preDispatch(Request_Abstract $request, Response_Abstract $response): void
    {
        set_exception_handler(function ($e) use ($request, $response) {
            $this->exceptionHandler($e, $request, $response);
        });
    }

    /**
     * 异常处理器.
     *
     * @param Throwable $e
     * @param $request
     * @param $response
     * @throws Exception
     */
    #[NoReturn] public function exceptionHandler(Throwable $e, $request, $response): void
    {
        Log::$customFileName = LOG_PATH."/errors/";
        Log::error($e->getMessage()." ".$e->getTraceAsString());
    }
}
